* Import dataset
use "/Users/Enrijeta/Dropbox/Hood et al. Registered Indies/SPPQ/SPPQ_replication/nc/nc_dta_vf.dta"


************************
*** Recode variables ***
************************

* registration party (voter file): 1 = NPA; 0 = Dem/Rep; drop Other
* source: voter file
gen reg_npa_vf = .
replace reg_npa_vf = 1 if party_code == "N" 
replace reg_npa_vf = 0 if (party_code == "D" | party_code == "R")

tab reg_npa_vf

* Age: 18-99
* source: voter file
destring age1, replace force

gen age2 = age1

tab age2

* Gender: 1 = female; 0 = male; drop other (36 cases)
* source: voter file
gen female = .
replace female = 1 if sex == "F"
replace female = 0 if sex == "M" | sex == "m"

tabulate female


* race2: 1 = white non-hispanic, 0 = others
* source: voter file
gen white = .
replace white = 1 if ethnic_cod == "W"
replace white = 0 if ethnic_cod == "A" | ethnic_cod == "B" | ethnic_cod == "H"| ///
ethnic_cod == "I" | ethnic_cod == "O"

tab white

* Education: 1 = high school or less; 2 = some college; 3 = college; 4 = graduate 
destring education, replace force

gen education2 = .
replace education2 = 1 if education < 3
replace education2 = 2 if education == 3
replace education2 = 3 if education == 4
replace education2 = 4 if education > 4

tab education2


* Did you move to NC from another state? 1 = yes; 0 = no, drop idk
destring moved, replace force

gen moveto_state = .
replace moveto_state = 1 if moved == 1
replace moveto_state = 0 if moved == 2
tab moveto_state

* resident: 1 = yes; 0 = no; drop IDK
gen resident = .
replace resident = 1 if  resident_of_state == 1
replace resident = 0 if  resident_of_state == 2
tab resident


* Attend local political meetings (such as school board or city council): 
* 1 = yes; 0 = no; drop IDK
destring pol_part_1, replace force

gen pol_meeting = .
replace pol_meeting = 1 if pol_part_1 == 1
replace pol_meeting = 0 if pol_part_1 == 2

tab pol_meeting


* Put up a political sign (such as a lawn sign or bumper sticker): 
* 1 = yes; 0 = no; drop IDK
destring pol_part_2, replace force

gen pol_sign = .
replace pol_sign = 1 if pol_part_2 == 1
replace pol_sign = 0 if pol_part_2 == 2

tab pol_sign


* Work for a candidate or campaign:
* 1 = yes; 0 = no; drop IDK
destring pol_part_3, replace force

gen pol_campaign = .
replace pol_campaign = 1 if pol_part_3 == 1
replace pol_campaign = 0 if pol_part_3 == 2

tab pol_campaign


* Attend a political protest, march or demonstration: 
* 1 = yes; 0 = no; drop IDK
destring pol_part_4, replace force

gen pol_protest = .
replace pol_protest = 1 if pol_part_4 == 1
replace pol_protest = 0 if pol_part_4 == 2

tab pol_protest


* Contact a public official:
* 1 = yes; 0 = no; drop IDK
destring pol_part_5, replace force

gen pol_official = .
replace pol_official = 1 if pol_part_5 == 1
replace pol_official = 0 if pol_part_5 == 2

tab pol_official

* Donate money to a candidate, campaign, or political organization:
* 1 = yes; 0 = no; drop IDK
destring pol_part_6, replace force

gen pol_donate = .
replace pol_donate = 1 if pol_part_6 == 1
replace pol_donate = 0 if pol_part_6 == 2

tab pol_donate

* Political engagement scale
* Crombach alpha for engagement scale items: alpha = 0.62
alpha pol_meeting pol_sign pol_campaign pol_protest pol_official pol_donate

gen pol_engagement = pol_meeting + pol_sign + pol_campaign + pol_protest + ///
pol_official + pol_donate
tab pol_engagement

* recode political engagement scale: 0 --> 5 (combine 5 and 6 due to low cases)
gen pol_engagement5 = pol_engagement
replace pol_engagement5 = 5 if pol_engagement == 6

tab pol_engagement5

* PID strength: 1 = strong republican ---> 7 = strong democrat
destring rstrength, replace force
encode dstrength, gen(dstrength1)

replace rstrength = . if rstrength == 3
replace dstrength1 = . if dstrength1 == 3

gen pid_strength = .
replace pid_strength = 1 if rstrength == 1
replace pid_strength = 2 if rstrength == 2
replace pid_strength = 3 if plean == "1" & missing(pid_strength)
replace pid_strength = 5 if plean == "2" & missing(pid_strength)
replace pid_strength = 4 if plean == "3" & missing(pid_strength)
replace pid_strength = 6 if dstrength1 == 2 & missing(pid_strength)
replace pid_strength = 7 if dstrength1 == 1 & missing(pid_strength)
tab pid_strength


* pid3: 1 = Independent (pure); 2 = Republican (leaners included), 
* 3= Democrat (leaners included)
gen pid3 = .
replace pid3 = 1 if pid_strength == 4
replace pid3 = 3 if pid_strength == 5 |pid_strength == 6 | pid_strength == 7
replace pid3 = 2 if pid_strength == 2 | pid_strength == 1 | pid_strength == 3
tab pid3


* pid strength: 1 = pure; 2 = leaning; 3 = weak; 4 = strong
gen pid_strength_cat4 = .
replace pid_strength_cat4 = 1 if pid_strength == 4
replace pid_strength_cat4 = 2 if pid_strength == 3 | pid_strength == 5
replace pid_strength_cat4 = 3 if pid_strength == 2 | pid_strength == 6
replace pid_strength_cat4 = 4 if pid_strength == 1 | pid_strength == 7
label define pid_strength_cat4_label 1 "Pure" 2 "Leaning" 3 "Weak" 4 "Strong"
label values pid_strength_cat4 pid_strength_cat4_label
tab pid_strength_cat4


* Political interest: 1 (hardly at all) --> 4 (most of the time)
destring political_interest, replace force

gen political_interest2 = .
replace political_interest2 = . if political_interest == 9
gen pol_interest2 = 5 - political_interest

tab pol_interest2


* Ideology: 1 = conservative --> 7 = liberal 
destring ideology, replace force

gen ideo7 = 8 - ideology
tab ideo7


* Ideology strength: 1= no ideologue; 2 = weak ideologist; 3 = ideologues; 4 = strong ideologues 

gen ideo_strength = .
replace ideo_strength = 1 if ideo7 == 4
replace ideo_strength = 2 if ideo7 == 3 | ideo7 == 5
replace ideo_strength = 3 if ideo7 == 2 | ideo7 == 6
replace ideo_strength = 4 if ideo7 == 1 | ideo7 == 7

tab ideo_strength


* Nowadays, it is difficult for two people to talk about politics without getting into an argument:
* 1 = agree; 0 = disagree; drop IDK
destring politics_1, replace force

gen talk_politics = .
replace talk_politics = 1 if politics_1 == 1
replace talk_politics = 0 if politics_1 == 2

tab talk_politics


* Nowadays, it is difficult to openly share your political views. 
* 1 = agree; 0 = disagree; drop IDK
destring politics_2, replace force

gen shareviews_politics = .
replace shareviews_politics = 1 if politics_2 == 1
replace shareviews_politics = 0 if politics_2 == 2

tab shareviews_politics


* I prefer to surround myself with politically like minded people
* 1 = agree; 0 = disagree; drop IDK
destring politics_3, replace force

gen likeminded_politics = .
replace likeminded_politics = 1 if politics_3 == 1
replace likeminded_politics = 0 if politics_3 == 2

tab likeminded_politics



* Usually vote in primary election: 1 = yes; 0 = no
destring primary_vote, replace force

replace primary_vote = . if primary_vote == 9

gen vote_primary = cond(primary_vote == 1, 1, cond(primary_vote == 2, 0, .))

tab vote_primary


*  How important it is to vote in primary election: 1 (not important) --> 4 (very important)
destring importance_primary, replace force

replace importance_primary = . if importance_primary == 9

gen vote_primary_imp = cond(importance_primary == 4, 1, ///
cond(importance_primary == 3, 2, cond(importance_primary == 2, 3, ///
cond(importance_primary == 1, 4, .))))

tab vote_primary_imp


* How important it is to vote in primary election: 1 = very important; 0 = not important
gen vote_primary_imp2 = (vote_primary_imp > 3)
tab vote_primary_imp2

* Countycode
encode ai_county_name, gen(countycode1)
*label var countycode1 ""
*label drop countycode1


* registration length in years
destring reg_date, replace force

gen regyear_length = 2023 - reg_date
tab regyear_length


* NC indicator
gen fl_state = 0


* keep only florida residents
keep if resident == 1



***************
*** Table 1 ***
***************

tabulate pid_strength reg_npa_vf [aw=weight], col

svyset [pw=weight]
svy: tabulate reg_npa_vf, col count percent


* Keep only the needed variables for merging
keep reg_npa_vf age2 female white education2 ///
                  moveto_state pol_engagement5 pid_strength_cat4 ///
                  pol_interest2 ideo_strength regyear_length ///
                  talk_politics shareviews_politics likeminded_politics ///
                  vote_primary vote_primary_imp2 countycode1 weight fl_state
				  

save "/Users/Enrijeta/Dropbox/Hood et al. Registered Indies/SPPQ/stata/pooled/nc_sppq1.dta", replace




*******************************************
*** Table 2: Logistic regression models ***
*******************************************

* Model 1 (Full model)
logistic reg_npa_vf age2 female white education2 moveto_state regyear_length ///
				  talk_politics shareviews_politics likeminded_politics /// 
				  vote_primary vote_primary_imp2 pol_engagement5 pol_interest2 ///
				  ideo_strength pid_strength_cat4  [pweight=weight], vce(cluster countycode1)
				  
eststo m1_full_nc



* Export results to Excel
estout  m1_full_nc using model081001.txt, cells(b(star fmt(3)) ///
se(par fmt(3))) stats(chi2 loglikelihood N aic bic, fmt(3 0 1) label(WaldX2 Log N aic bic)) ///
varlabels(_cons "Intercept" ///
age2 "Age" female "Female" white "White non-Hispanic" education2 "Education" ///
moveto_state "Moved to FL/NC" ///
pol_engagement5 "Political engagement" pid_strength_cat4 "PID strength (pure to strong partisan)" ///
pol_interest "Political interest" ideo_strength "Ideology strength (no strong ideologue)" regyear_length "Registration length" ///
talk_politics "Talk politics (difficult)" shareviews_politics "Share views on politics (difficult)" likeminded_politics "Surrounded by like minded people" ///
vote_primary "Vote in primary (usually)" vote_primary_imp2 "Voting in primary is important") 



* Calculate predicted probabilities for the model
margins, at(age=(19 99))
margins, at(education2=(1 2 3 4))
margins, at(pid_strength_cat4=(1 2 3 4))


* weighted summary statistics (get mean and sd)
svy: mean age female white education2 moveto_state pol_engagement5  pid_strength_cat4 ///
          pol_interest2 ideo_strength  regyear_length talk_politics shareviews_politics ///
		  likeminded_politics vote_primary vote_primary_imp2
estat sd


* get n and min/max
sum age female white education2 moveto_state pol_engagement5  pid_strength_cat4 ///
          pol_interest2 ideo_strength regyear_length talk_politics shareviews_politics ///
		  likeminded_politics vote_primary vote_primary_imp2

















